package ru.ifmo.genetics.factory.string;

import org.uncommons.maths.number.NumberGenerator;
import org.uncommons.watchmaker.framework.factories.AbstractCandidateFactory;

import java.util.List;
import java.util.Random;

/**
 * @author Roman Kolganov
 *         02.12.11
 */
public class DistributionBasedStringFactory extends AbstractCandidateFactory<String> {

    private final NumberGenerator<Integer> maxLength;
    private final List<Character> alphabet;
    private final NumberGenerator<Integer> symbolNumberGenerator;

    public DistributionBasedStringFactory(NumberGenerator<Integer> maxLength, List<Character> alphabet,
                                          NumberGenerator<Integer> symbolNumberGenerator) {
        this.maxLength = maxLength;
        this.alphabet = alphabet;
        this.symbolNumberGenerator = symbolNumberGenerator;
    }

    public String generateRandomCandidate(Random rng) {
        int len = rng.nextInt(maxLength.nextValue()) + 1;
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < len; i++) {
            res.append(alphabet.get(symbolNumberGenerator.nextValue()));
        }
        return res.toString();
    }

}
